package com.yu.control.userinfo;

import com.alibaba.fastjson.JSON;
import com.yuw.bean.userinfo.UserInfoBean;
import com.yuw.model.userinfo.ModelUserInfo;
import com.yuw.utils.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ServletUserinfoQuery", value = "/ServletUserinfoQuery")
public class ServletUserinfoQuery extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取封装数据的对象
        UserInfoBean userInfoBean = MyUtils.convertRequest2Bean(request, UserInfoBean.class);
        System.out.println(userInfoBean);
        //业务逻辑处理
        ModelUserInfo newInstance = MyUtils.getNewInstance(ModelUserInfo.class);
        List<UserInfoBean> userInfoBeans = newInstance.queryUserinfoByParams(userInfoBean);
        //将集合转换为json字符串
        //使用外部工具fastjson
        String strJson = JSON.toJSONStringWithDateFormat(userInfoBeans, "yyyy-MM-dd HH:mm:ss");
        PrintWriter writer = response.getWriter();
        writer.write(strJson);
        writer.flush();
    }
}
